package com.hospital.workstation.controller;

import java.io.IOException;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.hospital.workstation.domain.bo.ElectronicMonitorOneInfoBo;
import com.hospital.workstation.domain.vo.ElectronicMonitorOneInfoVo;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.hospital.common.annotation.RepeatSubmit;
import com.hospital.common.annotation.Log;
import com.hospital.common.core.controller.BaseController;
import com.hospital.common.core.domain.PageQuery;
import com.hospital.common.core.domain.R;
import com.hospital.common.core.validate.AddGroup;
import com.hospital.common.core.validate.EditGroup;
import com.hospital.common.core.validate.QueryGroup;
import com.hospital.common.enums.BusinessType;
import com.hospital.common.utils.poi.ExcelUtil;
import com.hospital.workstation.domain.vo.ElectronicMonitorTwoInfoVo;
import com.hospital.workstation.domain.bo.ElectronicMonitorTwoInfoBo;
import com.hospital.workstation.service.IElectronicMonitorTwoInfoService;
import com.hospital.common.core.page.TableDataInfo;

/**
 * B表 药品不良反应和事件报告表
 *
 * @author hospital
 * @date 2024-05-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workstation/monitorTwoInfo")
public class ElectronicMonitorTwoInfoController extends BaseController {

    private final IElectronicMonitorTwoInfoService iElectronicMonitorTwoInfoService;

    /**
     * 根据患者流水号查询B表信息
     *
     * @param medicalRecordNo 患者流水号
     * @return
     */
    @GetMapping("/medicalRecordNo/{medicalRecordNo}")
    public R<ElectronicMonitorTwoInfoVo> getMonitorTwoInfo(@NotNull(message = "患者流水号不能为空")
                                                           @PathVariable Integer medicalRecordNo) {
        return iElectronicMonitorTwoInfoService.queryMonitorTwoInfo(medicalRecordNo);
    }

    /**
     * 保存B表 药品不良反应/事件报告表
     */
    //@SaCheckPermission("workstation:monitorTwoInfo:add")
    @Log(title = "B表 药品不良反应/事件报告表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ElectronicMonitorTwoInfoBo bo) {
        return iElectronicMonitorTwoInfoService.insertByBo(bo);
    }

    /**
     * 检测表流水号判重
     *
     * @param monitorRecordNo 检测表流水号
     * @return
     */
    @GetMapping("/exist/{monitorRecordNo}")
    public R<Boolean> isExist(@NotNull(message = "检测表流水号不能为空")
                              @PathVariable Integer monitorRecordNo) {
        return iElectronicMonitorTwoInfoService.isExistMonitorRecordNo(monitorRecordNo);
    }

    /**
     * 查询B表 药品不良反应/事件报告表列表
     */
    @GetMapping("/list")
    public TableDataInfo<ElectronicMonitorTwoInfoVo> list(ElectronicMonitorTwoInfoBo bo, PageQuery pageQuery) {
        return iElectronicMonitorTwoInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 根据id查询B表
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<ElectronicMonitorTwoInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                                 @PathVariable Long id) {
        return R.ok(iElectronicMonitorTwoInfoService.queryById(id));
    }

    /**
     * 驳回
     *
     * @param bo
     */
    //@SaCheckPermission("workstation:monitorOneInfo:query")
    @PutMapping("/reject")
    public R<Void> reject(@Validated(AddGroup.class) @RequestBody ElectronicMonitorTwoInfoBo bo) {
        return iElectronicMonitorTwoInfoService.reject(bo);
    }

    /**
     * 删除B表 药品不良反应/事件报告表
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("workstation:monitorTwoInfo:remove")
    @Log(title = "B表 药品不良反应/事件报告表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iElectronicMonitorTwoInfoService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 下载PDF
     *
     * @param response
     * @param ids
     * @throws IOException
     */
    @GetMapping("/download/{ids}")
    public void download(HttpServletResponse response, @PathVariable Long[] ids) throws IOException {
        iElectronicMonitorTwoInfoService.exportFile(response, ids);
    }
}
